package org.idea.spring.dependency.inject.field;

import org.idea.spring.dependency.inject.setter.PersonHolder;
import org.idea.spring.ioc.bean.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

import javax.annotation.Resource;

/**
 * @Author linhao
 * @Date created in 10:48 下午 2021/4/22
 */
public class AnnotationDependencyInjectDemo {

    @Autowired
    private PersonHolder personHolder2;

    @Resource
    private PersonHolder personHolder;

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(AnnotationDependencyInjectDemo.class);
        applicationContext.refresh();
        AnnotationDependencyInjectDemo annotationDependencyInjectDemo = applicationContext.getBean(AnnotationDependencyInjectDemo.class);
        System.out.println(annotationDependencyInjectDemo.personHolder);
        System.out.println(annotationDependencyInjectDemo.personHolder2);
        //这里面的两个bean都是同一个，因为bean的作用域是一致相同的
        System.out.println(annotationDependencyInjectDemo.personHolder == annotationDependencyInjectDemo.personHolder2);
        applicationContext.close();
    }

    @Bean
    public PersonHolder personHolder(){
        return new PersonHolder(new Person(1,"idea"));
    }
}
